using System.Collections.Generic;
using System.Linq;
using ShellLight.Contract;

namespace ShellLight
{
    //todo far from complete. should be unittestet to
    public static class CommandFinder
    {
        public static List<UICommand> Find(string searchText, List<UICommand> commands, out string parameter)
        {
            parameter = string.Empty;
            var foundCommands = new List<UICommand>();
            string criteria;
            criteria = searchText;
            if (criteria.Contains(" "))
            {
                var parts = criteria.Split(' ');
                criteria = parts[0];
                parameter = parts[1];
            }

            if (!string.IsNullOrEmpty(criteria))
            {
                var result = from c in commands
                             where c.Name.ToLower().Contains(criteria.ToLower())
                             select c;

                foundCommands = result.ToList();
            }
            return foundCommands;
        }

        public static List<UICommand> CreateTop3Commands(List<UICommand> commands)
        {
            var topScoreCommands = new List<UICommand>();
            var result = from c in commands where c.Score > 0 orderby c.Score descending select c;
            //MessageBox.AfterShow(result.Count().ToString());
            if (result.Count() > 0)
            {
                topScoreCommands = result.Take(3).ToList();
                //MessageBox.AfterShow(top3Commands[0].Name);
            }
            return topScoreCommands;
        }
    }
}